package com.wonderful.eyepetizer.ui.pages.hot

import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.viewModelScope
import com.wonderful.eyepetizer.app.base.BaseViewModel
import com.wonderful.eyepetizer.data.entity.bean.Item
import com.wonderful.eyepetizer.data.entity.bean.Tab
import com.wonderful.eyepetizer.data.repo.MainRepo
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * Desc:
 * @Author: wonderful
 * @Time: 2024/2/23 16:14
 */
@HiltViewModel
class HotViewModel @Inject constructor(private val mRepo: MainRepo) : BaseViewModel() {
    var tabList = mutableStateListOf<Tab>()
    var tabIndex = mutableIntStateOf(0)

    private fun getTabList() {
        viewModelScope.launch {
            mRepo.getTabList().data?.let {
                val tabs = it.tabInfo.tabList
                if (tabs.isNotEmpty()) {
                    tabList.clear()
                    tabList.addAll(tabs)
                }
            }
        }
    }

    var itemList = mutableStateListOf<Item>()
    var refreshing = mutableStateOf(false)

    fun getTabData(url: String, isRefresh: Boolean = false) {
        if (itemList.isEmpty() || isRefresh) {
            viewModelScope.launch {
                if (isRefresh) {
                    refreshing.value = true
                }
                mRepo.getHotTabData(url).data?.itemList?.let {
                    itemList.clear()
                    itemList.addAll(it)
                }
                if (isRefresh) {
                    refreshing.value = false
                }
            }
        }
    }

    init {
        getTabList()
    }
}